package org.csu.onlineedubackend.controller;

import org.csu.onlineedubackend.entity.ForumComment;
import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.service.ForumCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.List;

@RestController
@RequestMapping("/api/forum/comment")
public class ForumCommentController {
    @Autowired
    private ForumCommentService forumCommentService;

    /**
     * 發表評論
     */
    @PostMapping("/create")
    public boolean createComment(@RequestBody ForumComment comment) {
        return forumCommentService.createComment(comment);
    }

    /**
     * 刪除評論
     */
    @DeleteMapping("/delete/{commentId}")
    public boolean deleteComment(@PathVariable Integer commentId, @RequestParam Integer userId) {
        return forumCommentService.deleteComment(commentId, userId);
    }

    /**
     * 分頁查詢某帖子的所有評論
     */
    @GetMapping("/list")
    public IPage<ForumComment> getCommentsByPost(@RequestParam int pageNum, @RequestParam int pageSize, @RequestParam Integer postId) {
        Page<ForumComment> page = new Page<>(pageNum, pageSize);
        return forumCommentService.getCommentsByPost(page, postId);
    }

    /**
     * 查詢某評論的所有回覆
     */
    @GetMapping("/replies/{parentId}")
    public List<ForumComment> getRepliesByParent(@PathVariable Integer parentId) {
        return forumCommentService.getRepliesByParent(parentId);
    }
    
    /**
     * 獲取帖子的總評論數
     */
    @GetMapping("/count/{postId}")
    public int getCommentCountByPost(@PathVariable Integer postId) {
        return forumCommentService.getCommentCountByPost(postId);
    }

    /**
     * 獲取用戶帖子收到的總評論數
     */
    @GetMapping("/receivedCount/{userId}")
    public CommonResponse<Integer> getUserReceivedCommentCount(@PathVariable Integer userId) {
        try {
            int count = forumCommentService.getUserReceivedCommentCount(userId);
            return CommonResponse.createForSuccess(count);
        } catch (Exception e) {
            return CommonResponse.createForError("查詢用戶評論數失敗：" + e.getMessage());
        }
    }
} 